package com.carlinfo.common.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


/**
 * 读取 json 文件
 * */
public class IOUtil
{
	private Logger logger = LogManager.getLogger();
	/**
	 *	读取json文件 
	 *	文件中放的是字符 
	 */
	public String readFile(File souFile)
	{
		try
		{
			InputStream in = new FileInputStream(souFile);
			String readFile = this.readFile(in);
			return readFile;
		} catch (FileNotFoundException e)
		{
			logger.error("读取文件内容失败了",e);
		}
		return "";
	}
	
	/**
	 *  直接返回对象 读取
	 */
	public String readFile(InputStream in)
	{
		/*	存储结果	*/
		StringBuffer sb = new StringBuffer();
		
		BufferedReader br = null ;
		try
		{
			br = new BufferedReader(new InputStreamReader(in,"UTF-8"));
			
			String line = "";
			while((line= br.readLine()) != null )
			{
				sb.append(line + "\r\n");
			}
		} catch (IOException e)
		{
			logger.error("读取文件内容失败了",e);
		} finally
		{
			try
			{
				if(br != null)
				{
					br.close();
					br = null;
				}
			} catch (IOException e)
			{
				logger.error("关闭水管失败了",e);
			}
		}
		return sb.toString();
	}
	public static void main(String[] args)
	{
		IOUtil ioUtil = new IOUtil() ;
		File souFile = new File("D:\\新建文件夹\\src_config\\config.json");
		String fileStr = ioUtil.readFile(souFile);
		System.out.println("==基于路径==>" + fileStr);
		
		InputStream in = IOUtil.class.getClassLoader().getClass().getResourceAsStream("/config.json");
		fileStr = ioUtil.readFile(in);
		System.out.println("=="+fileStr);
	}
}
